home *** CD-ROM | disk | FTP | other *** search
- Path: svnews.ubinet.ubs.com!ubszh!ubszh!jis
- From: ain.johnston@ubs.com (Johnston Ian (by ubsswop))
- Newsgroups: comp.lang.c++
- Subject: Re: So I have this pointer-to-member function...
- Date: 31 Jan 1996 10:39:33 GMT
- Organization: UBS
- Distribution: world
- Message-ID: <4engt5$plq@ubszh.fh.zh.ubs.com>
- References: <d4c1.smail.smayo@tiac.net>
- NNTP-Posting-Host: nol2179.fh.zh.ubs.com
-
- In article <d4c1.smail.smayo@tiac.net>, smayo@tiac.net (Scott Mayo) writes:
- |> I'm trying to do something in C++ which instinct tells me shouldn't be hard to
- |> arrange, but the compiler isn't cooperating.
- |>
- |> I have a class, call it C, whose purpose in life is to acquire an input
- |> stream, parse it into tokens, and then have the tokens tell it which member
- |> functions to call. For token "A" call member function A, and so on. An array
- |> of struct {char *token; void (C::*f)();} fits the bill well enough to manage
- |> that.
- |>
- |> Now the tricky part. I want to derive class C1 and C2 from C. And I want C1
- |> (and C2, etc) to have their own such table for mapping tokens to member
- |> functions; in the absence of a match, I want to go up and search C's table and
- |> use what I find there.
- |>
- |> "Ah," I innocently thought, "I'll just make each array of struct a static
- |> variable inside the class definition it goes with, and then it will be easy to
- |> automatically search the right table at the right time."
- |>
- |> The compiler will have none of it. It takes one glance at the static struct
- |> {char*name; void (C::*f)();} list[] = {"A", C::A}; and has a hissy fit at the
- |> attempt to assign initializers. It's fine outside the class definition; it
- |> won't work inside.
- |>
- |> I can clearly just put the arrays outside the class definitions, and just
- |> teach the class "search" function to know which array to go after, but it
- |> seems crass. That array is logically a part of the class behaviour, and if I
- |> end up creating a lot of these subclasses, I don't want to rely on
- |> remembering to tell each search function which magic array to use.
- |>
- |> Suggestions? Am I missing something simple and useful here? Thanks. Mail
- |> appreciated, posting might be missed.
- |>
-
-
- How about this:
-
-
-
- struct C
- {
- typedef void (C::*TokFunc)();
-
- struct Table
- {
- char *name;
- TokFunc func;
- };
-
- void tokIf();
- void tokWhile();
-
- static Table c_lookup[];
- };
-
-
- C::Table C::c_lookup[] =
- {
- { "if", &C::tokIf },
- { "while", &C::tokWhile }
- };
-
-
- struct C1 : public C
- {
- typedef void (C1::*TokFunc)();
-
- struct Table
- {
- char *name;
- TokFunc func;
- };
-
- void tokCase();
- void tokDefault();
-
- static Table c1_lookup[];
- };
-
- C1::Table C1::c1_lookup[] =
- {
- { "case", &C1::tokCase },
- { "default", &C1::tokDefault }
- };
-
-
-
- Now each class's search function uses its own local table, and calls its
- base class's search function if the token is not found.
-
- The search functions should actually call the token functions when they
- find a match.
-
- Ian
-